package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lzx.iteam.CRMTextSelectActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.CRMDetailBean;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.widget.RoundImageView;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 08月 01日
 * Time: 下午3:28
 *
 * @QQ : 1234567890
 * **********************************
 */
public class CRMDetailAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<CRMDetailBean> mListData;

    public CRMDetailAdapter(Context mContext, ArrayList<CRMDetailBean> mListData) {
        this.mContext = mContext;
        this.mListData = mListData;
    }

    public void bindData(ArrayList<CRMDetailBean> mListData){
        this.mListData = mListData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.crm_detail_item_layout,null);
            holder.mTopLayout = (LinearLayout) convertView.findViewById(R.id.crm_detail_item_top);
            holder.mBottomLayout = (RelativeLayout) convertView.findViewById(R.id.crm_detail_item_bottom_layout);
            holder.mClueLayout = (LinearLayout) convertView.findViewById(R.id.crm_detail_item_clue);
            holder.tvClue = (TextView) convertView.findViewById(R.id.crm_detail_item_clue_num);
            holder.topView = convertView.findViewById(R.id.crm_detail_item_view);
            holder.tvTop = (TextView) convertView.findViewById(R.id.crm_detail_item_tv_top);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.crm_detail_item_title);
            holder.tvContent = (TextView) convertView.findViewById(R.id.crm_detail_item_content);
            holder.ivImage = (RoundImageView) convertView.findViewById(R.id.crm_detail_item_image);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        CRMDetailBean data = mListData.get(position);

        if (data.getType() == 1){ // 判断是不是标题
            holder.mTopLayout.setVisibility(View.VISIBLE);
            holder.mBottomLayout.setVisibility(View.GONE);
            holder.tvTop.setText(data.getTopTitle());
            if (position == 0){ // 判断是不是基本信息
                holder.topView.setVisibility(View.GONE);
            }else{
                holder.topView.setVisibility(View.VISIBLE);
            }
        }else{
            holder.mTopLayout.setVisibility(View.GONE);
            holder.mBottomLayout.setVisibility(View.VISIBLE);

            holder.tvTitle.setText(data.getTitle());
            if (data.getType() == 2){ // 跟进状态
                holder.tvContent.setText(CRMTextSelectActivity.status[data.getIndex()]);
            }else if (data.getType() == 3){ // 性别
                holder.tvContent.setText(CRMTextSelectActivity.sex[data.getIndex()]);
            }/*else if (data.getType() == 4){ // 地址
                holder.tvContent.setText(data.getContent());
            }*/else if (data.getType() == 5){ // 来源
                holder.tvContent.setText(CRMTextSelectActivity.source[data.getIndex()]);
            }/*else if (data.getType() == 6){ // 备注
                holder.tvContent.setText(data.getContent());
            }*/else if (data.getType() == 7){
                holder.tvContent.setText(CRMTextSelectActivity.stage[data.getIndex()]);
            } else if (data.getType() == 9){
                holder.tvContent.setText(CRMTextSelectActivity.type[data.getIndex()]);
            } else if (data.getType() == 11){
                holder.tvContent.setText(CRMTextSelectActivity.chance_origin[data.getIndex()]);
            } else if (data.getType() == 8){
                holder.tvContent.setText(DateUtil.formatDate(Long.parseLong(data.getContent()) * 1000));
            } else if (data.getType() == 12){
                holder.tvContent.setText(DateUtil.getDetailDate(Long.parseLong(data.getContent()) * 1000));
            } else if (data.getType() == 13){
                holder.tvContent.setText(CRMTextSelectActivity.stage_num[data.getIndex()]);
            } else{
                if (!StringUtil.isEmpty(data.getContent())){
                    holder.tvContent.setText(data.getContent());
                }
            }

            if (!StringUtil.isEmpty(data.getImage())){ // 判断有没有头像
                holder.ivImage.setVisibility(View.VISIBLE);
                ImageLoaderInterface.imageLoader.displayImage(data.getImage(), holder.ivImage, ImageLoaderInterface.optionHeadImage);

            }else{
                holder.ivImage.setVisibility(View.GONE);
            }
            if (data.getType() == 10){
                holder.mClueLayout.setVisibility(View.VISIBLE);
                holder.tvClue.setText(data.getIndex() + "");
            }else{
                holder.mClueLayout.setVisibility(View.GONE);
            }

        }
        return convertView;
    }

    class ViewHolder{

        LinearLayout mTopLayout,mClueLayout;
        RelativeLayout mBottomLayout;
        TextView tvTop,tvTitle,tvContent,tvClue;
        RoundImageView ivImage;
        View topView;

    }

}
